﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tallerista.Domain
{
    public interface IDateRange
    {
        DateTime StartDate { get; }
        DateTime? EndDate { get; }
        bool Contains(DateTime valueToFind);
        int Duracion { get; }

    }

    public class DateRange : IDateRange
    {
        private readonly DateTime _start;
        private DateTime? _end;

        public DateRange(DateTime start, DateTime? end)
        {
            _start = start;
            _end = end;
        }

        public DateTime StartDate
        {
            get { return _start; }
        }

        public DateTime? EndDate
        {
            get { return _end; }
            set { _end = value; }
        }

        public bool Contains(DateTime valueToFind)
        {
            return valueToFind.CompareTo(StartDate) >= 0 &&
                   valueToFind.CompareTo((_end.HasValue) ? _end.Value : DateTime.MaxValue) <= 0;

        }

        /// <summary>
        /// Devuelve el lapso de tiempo en años
        /// </summary>
        public int Duracion
        {
            get
            {
                var endDate = (_end.HasValue) ? _end.Value : DateTime.Today;
                return (endDate - _start).Days / 366;
            }
        }
    }
}
